package com.jh.jingfei.main.ui;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jh.jingfei.MyApplication;
import com.jh.jingfei.R;
import com.jh.jingfei.main.event.StartBrotherEvent;
import com.jh.jingfei.mine.ItemBottomSheetDialogFragment;
import com.jh.jingfei.mine.ui.SettingFragment;
import com.jh.jingfei.models.Account;
import com.jh.jingfei.widget.MyToolbar;
import com.makeramen.roundedimageview.RoundedImageView;

import org.greenrobot.eventbus.EventBus;

import me.yokeyword.fragmentation_swipeback.SwipeBackFragment;

/**
 * @author lxb
 *
 * Mine (我的主页面)
 */
public class MineFragment extends SwipeBackFragment implements ItemBottomSheetDialogFragment.Listener {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    private Account account;
    private MyToolbar toolbar;
    private TextView usernameTv;
    private TextView logout_txt;
    private RoundedImageView userAvatarIv;

    public MineFragment() {
        // Required empty public constructor
    }

    public static MineFragment newInstance(String param1, String param2) {
        MineFragment fragment = new MineFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_mine, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        toolbar = view.findViewById(R.id.mine_top_toolbar);
        usernameTv = view.findViewById(R.id.user_name);
        userAvatarIv = view.findViewById(R.id.user_avatar);
        logout_txt = view.findViewById(R.id.logout_txt);

        toolbar.setTitle(R.string.me_name);
        toolbar.setNavigationIcon(null);
        toolbar.setRightIconResource(R.drawable.ic_settings_black_24dp);

        usernameTv.setText("管理员");
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        toolbar.setOnRightMenuClickListener(new MyToolbar.OnRightMenuClickListener() {
            @Override
            public void onClick(View v) {
                SettingFragment settingFragment = SettingFragment.newInstance("","");
                EventBus.getDefault().post(new StartBrotherEvent(settingFragment));
            }
        });

        logout_txt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ItemBottomSheetDialogFragment itemListDialogFragment = ItemBottomSheetDialogFragment.newInstance(4);
                FragmentTransaction tf = getFragmentManager().beginTransaction();
                tf.setCustomAnimations(R.anim.bottom_sheet_dialog_in_to_top, R.anim.bottom_sheet_dialog_out_to_bottom);
                itemListDialogFragment.show(tf, "ItemBottomSheetDialogFragment");
            }
        });
    }

    private boolean isFirst = true;

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        Log.i("Mine", "onSupportVisible");
        if (isFirst) {
            isFirst = false;
            MyApplication app = (MyApplication) _mActivity.getApplication();

            account = app.getAccount();
            if (account != null) {
                usernameTv.setText(account.getName());
            }
        } else {

        }
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        Log.i("Mine", "onLazyInitView");
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onItemClicked(int position) {

    }
}
